package com.xin.marcus.utils;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.xin.marcus.entity.TestUser;
import com.xin.marcus.mapper.TestUserMapper;

import javax.annotation.Resource;
import java.util.ArrayList;


/**
 *
 * @Date 2023/3/11 23:12
 *
 */
public class ExcelReadListener extends AnalysisEventListener<TestUser> {

    @Resource
    private TestUserMapper testUserMapper;

    public ExcelReadListener(TestUserMapper mapper) {
        this.testUserMapper=mapper;
    }

    // 每读一样，会调用该invoke方法一次,在invoke可以操作使用读取到的数据
    @Override
    public void invoke(TestUser data, AnalysisContext context) {

        Long aLong = testUserMapper.selectMaxId();
        if (aLong!=null) {
            data.setTestUserid(aLong+1);
        }else {
            data.setTestUserid(Long.valueOf("1"));
        }
       testUserMapper.insert(data);
    }

    // 全部读完之后，会调用该方法
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

        System.out.println("导入成功");
    }
}